from django.conf import settings
from django.apps import apps
from django.contrib import admin
from .models import (Article, Tag, Category, Timeline,
                     Carousel, Silian, Keyword, FriendLink,
                     AboutBlog)

from blog.utils import MyHashEncryptor as MyHash,get_timestamp_rand
from django.template.defaultfilters import slugify
import re

@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
    # 这个的作用是给出一个筛选机制，一般按照时间比较好
    date_hierarchy = 'create_date'
    # 在查看修改的时候显示的属性，第一个字段带有<a>标签，所以最好放标题
    list_display = ('id', 'title', 'author', 'create_date', 'update_date', 'is_top')

    # 设置需要添加<a>标签的字段
    list_display_links = ('title',)

    # 激活过滤器，这个很有用
    list_filter = ('create_date', 'category', 'is_top')

    list_per_page = 50  # 控制每页显示的对象数量，默认是100

    filter_horizontal = ('tags', 'keywords')  # 给多选增加一个左右添加的框

    search_fields = ('author__username', 'title')
    # style_fields = {"body": "ckeditor"}
    exclude = ('author', 'views', 'slug', 'keywords')
    # 限制用户权限，只能看到自己编辑的文章
    def get_queryset(self, request):
        qs = super(ArticleAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

    def save_model(self,request, obj, form,change):

        if not change:
            num_sixteen = get_timestamp_rand() + "Article"
            slug_hash = MyHash.myencryped('md5', num_sixteen)

            obj.author = request.user
            # print(self.new_obj.author)
            obj.slug = slugify("-".join(re.findall(r'[0-9a-f]{8}', slug_hash)))
            old_body_150, old_summary = '', ''
        else:
            old_body_150 = Article.objects.filter(id=obj.id).values('body')[0].get('body')[:150]
            old_summary = Article.objects.filter(id=obj.id).values('summary')[0].get('summary')

        if (not change and obj.summary == getattr(settings, 'DEFALUT_SUMMARY')) \
                or (change and old_body_150 == old_summary \
                    and obj.body[:150] != old_summary \
                    or obj.summary == ''):
            obj.summary = obj.body[:150]
        # print(obj,type(obj))
        obj.save()

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        User = apps.get_model(settings.AUTH_USER_MODEL)
        if db_field.name == 'author':
            if request.user.is_superuser:
                kwargs['queryset'] = User.objects.filter(is_staff=True, is_active=True)
            else:
                kwargs['queryset'] = User.objects.filter(id=request.user.id)
        return super(ArticleAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
    list_display = ('name', 'id', 'slug')

    def save_models(self,request, obj, form,change):
        if not change:
            num_sixteen = get_timestamp_rand() + "Tag"
            slug_hash = MyHash.myencryped('md5', num_sixteen)
            self.obj.slug = slugify("-".join(re.findall(r'[0-9a-f]{8}', slug_hash)))
        obj.save()

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name', 'id', 'slug')

    def save_models(self, request, obj, form, change):
        if not change:
            num_sixteen = get_timestamp_rand() + "Category"
            slug_hash = MyHash.myencryped('md5', num_sixteen)
            self.obj.slug = slugify("-".join(re.findall(r'[0-9a-f]{8}', slug_hash)))
        obj.save()

# 自定义管理站点的名称和URL标题
admin.site.site_header = '网站管理'
admin.site.site_title = '博客后台管理'


@admin.register(Timeline)
class TimelineAdmin(admin.ModelAdmin):
    list_display = ('title', 'side', 'update_date', 'icon', 'icon_color',)
    fieldsets = (
        ('图标信息', {'fields': (('icon', 'icon_color'),)}),
        ('时间位置', {'fields': (('side', 'update_date', 'star_num'),)}),
        ('主要内容', {'fields': ('title', 'content')}),
    )
    date_hierarchy = 'update_date'
    list_filter = ('star_num', 'update_date')


@admin.register(Carousel)
class CarouselAdmin(admin.ModelAdmin):
    list_display = ('number', 'title', 'content', 'img_url', 'url')


@admin.register(Silian)
class SilianAdmin(admin.ModelAdmin):
    list_display = ('id', 'remark', 'badurl', 'add_date')


@admin.register(Keyword)
class KeywordAdmin(admin.ModelAdmin):
    list_display = ('name', 'id')


@admin.register(FriendLink)
class FriendLinkAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'link', 'create_date', 'is_active', 'is_show')
    date_hierarchy = 'create_date'
    list_filter = ('is_active', 'is_show')


@admin.register(AboutBlog)
class AboutBlogAdmin(admin.ModelAdmin):
    list_display = ('short_body', 'create_date', 'update_date')

    def short_body(self, obj):
        return '自由编辑 About 页面的内容，支持 markdown 语法。'

    short_body.short_description = 'AboutBlog'

    # 限制用户权限，只能超管可以编辑
    def get_queryset(self, request):
        qs = super(AboutBlogAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return None
